package appx.craft.droid.uc;

import java.util.ArrayList;
import java.util.LinkedList;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import appx.craft.droid.R;
import appx.craft.droid.bean.MessageBean;

public class MessageAdapater extends BaseAdapter{
	
	public LinkedList<MessageBean> mLinkListMessageBean;
	public Context mContext;
	private LayoutInflater mViewInflater;
	
	public MessageAdapater(Context vContext){
		mLinkListMessageBean = new LinkedList<MessageBean>();
		mContext = vContext;
		mViewInflater = LayoutInflater.from(vContext);
	}
	public void addMessage(MessageBean vTempBean){
		if(mLinkListMessageBean != null){
			mLinkListMessageBean.add(vTempBean);	
		}
	}
	
	public void addMessages(ArrayList<MessageBean> mMessageArrayList){
		
		if(mLinkListMessageBean != null){
			
			if(!mLinkListMessageBean.isEmpty()){
				
				mLinkListMessageBean.clear();
			}
			
			for(MessageBean mTempBean : mMessageArrayList){
				mLinkListMessageBean.add(mTempBean);	
			}
			notifyDataSetChanged();
				
		}
		
	}
	
	@Override
	public int getCount() {
		return mLinkListMessageBean.size();
	}

	@Override
	public Object getItem(int position) {
		return mLinkListMessageBean.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewMessageHolder mMessageHolder;
		MessageBean mCurrentMessage = mLinkListMessageBean.get(position);
		if(convertView == null){
			
			convertView = mViewInflater.inflate(R.layout.row_view_messages_items,parent,false);
			mMessageHolder = new ViewMessageHolder();
			
			mMessageHolder.mProfileIcon = (ImageView)convertView.findViewById(R.id.image_view_profile);
			
			mMessageHolder.mTextView_UserName = (TextView)convertView.findViewById(R.id.textView_user_name);
			mMessageHolder.mTextView_UserName.setMovementMethod(LinkMovementMethod.getInstance());
			mMessageHolder.mTextView_UserName.setLinkTextColor(Color.RED);
			
//			mMessageHolder.mTextView_Message = (TextView)convertView.findViewById(R.id.textView_messages);
			mMessageHolder.mTextView_timeSteamp = (TextView)convertView.findViewById(R.id.textView_timestemp);
			
			mMessageHolder.btn_request_approve = (Button)convertView.findViewById(R.id.btn_approve);
			mMessageHolder.btn_request_reject = (Button)convertView.findViewById(R.id.btn_reject); 
			
			convertView.setTag(mMessageHolder);
			
		}else{
			
			mMessageHolder = (ViewMessageHolder)convertView.getTag();
		}
		
		if(mCurrentMessage != null){
			
			 final String lblUserName = mCurrentMessage.getStrUserName();
			 final String lblMessage = mCurrentMessage.getStrMessage();
			
			SpannableStringBuilder mSpannableUserNameMessage = new SpannableStringBuilder();
			mSpannableUserNameMessage.append(lblUserName);
			mSpannableUserNameMessage.append(" ");
			mSpannableUserNameMessage.append(lblMessage);
			mSpannableUserNameMessage.setSpan(new NonUnderlinedClickableSpan(){
				@Override
				public void onClick(View widget) {
					super.onClick(widget);
					Toast.makeText(mContext,""+lblUserName,Toast.LENGTH_LONG).show();
					
				}
			},0, lblUserName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			
			mMessageHolder.mTextView_UserName.setText(mSpannableUserNameMessage);
			
//			mMessageHolder.mTextView_Message.setText(mCurrentMessage.getStrMessage());
			
			mMessageHolder.mTextView_timeSteamp.setText(mCurrentMessage.getStrTimeStemp());
			
			mMessageHolder.btn_request_approve.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {

					new Handler().postDelayed(new Runnable() {
						
						@Override
						public void run() {
							mLinkListMessageBean.remove(position);
							notifyDataSetChanged();
						}
					},100);
				}
			});
			
			mMessageHolder.btn_request_reject.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					new Handler().postDelayed(new Runnable() {
						
						@Override
						public void run() {
							mLinkListMessageBean.remove(position);
							notifyDataSetChanged();
						}
					},100);
				}
			});
			
			
		}
		
		return convertView;
	}
	
	class ViewMessageHolder{
		
		ImageView mProfileIcon;
		TextView mTextView_UserName;
		TextView mTextView_timeSteamp;
		
		Button btn_request_approve ;
		Button btn_request_reject;
	}
	
	public class NonUnderlinedClickableSpan extends ClickableSpan 
	{
		@Override
	        public void updateDrawState(TextPaint ds) {
	           ds.setColor(ds.linkColor);
	           ds.setUnderlineText(false); // set to false to remove underline
	        }
	 
		@Override
		public void onClick(View widget) {
		}
	}
}
